//
//  NewFeatureViewController.swift
//  SinaWeibo
//
//  Created by NeXTSTEP on 15/10/1.
//  Copyright © 2015年 NeXTSTEP. All rights reserved.
//

import UIKit


class NewFeatureViewController: UICollectionViewController {
    private let cellInentifier = "NewFeatureCell"
    
    @IBOutlet weak var layout: UICollectionViewFlowLayout!
    
    override func loadView() {
        super.loadView()
        print(view.bounds)
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        layout.itemSize = view.bounds.size
    }

    let imageCount = 4
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellInentifier, forIndexPath: indexPath) as! NewFeatureCell
        cell.imageIndex = indexPath.item
        if indexPath.item == imageCount - 1 {
            
        }
        return cell
    }
    
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        let path = collectionView.indexPathsForVisibleItems().last! as NSIndexPath
        let newCell = collectionView.cellForItemAtIndexPath(path) as! NewFeatureCell
        if path.item == imageCount - 1 {
            //调用显示动画
            newCell.showStartBtn()
        }
        
    }

}


class NewFeatureCell: UICollectionViewCell {
    
    
    @IBOutlet weak var startBtn: UIButton!
    @IBOutlet weak var iconView: UIImageView!
    
    var imageIndex: Int = 0 {
        didSet{
            iconView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
        }
    }
    
    
    //程序开发 要遵循单一性原则  单一模块处理单一模块对应的业务  代码逻辑更加清晰
    func showStartBtn(){
        self.startBtn.hidden = false
        self.startBtn.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(0.8, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            self.startBtn.transform = CGAffineTransformMakeScale(1, 1)
            }, completion: { (_) -> Void in
                print("OK")
        })
    }
    
    @IBAction func startAction(sender: AnyObject) {
        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchToRootVC, object: "Main")
    }
    
}
